package com.sskj.extract;

import java.io.*;
import java.util.Map;

public class Clear {
    public static void clear() {
        Map<String, String> map = Config.getMap();
        map.forEach((s, s2) -> {
            File file = new File(s + "/src/main/res/values/strings.xml");
            deleteDir(s + "/src/main/res/values-en-rUS");
            deleteDir(s + "/src/main/res/values-zh-rTW");
            deleteDir(s + "/src/main/res/values-ko");
            deleteDir(s + "/src/main/res/values-en");
            FileInputStream in = null;
            PrintWriter out = null;
            if (file.exists()){
                try {
                    //记录文件长度
                    Long fileLength = file.length();
                    //记录读出来的文件的内容
                    byte[] fileContext = new byte[fileLength.intValue()];
                      in = new FileInputStream(file);
                    //读出文件全部内容(内容和文件中的格式一致,含换行)
                    in.read(fileContext);
                    // 避免出现中文乱码
                    String str = new String(fileContext, "utf-8");

                    str = str.replaceAll("<strin[\\s\\S].*>\\n","");

                    //再把新的内容写入文件
                    out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));

                    out.write(str);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        out.flush();
                        out.close();
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    /**
     * 迭代删除文件夹
     * @param dirPath 文件夹路径
     */
    public static void deleteDir(String dirPath)
    {
        File file = new File(dirPath);
        if(file.isFile())
        {
            file.delete();
        }else
        {
            File[] files = file.listFiles();
            if(files == null)
            {
                file.delete();
            }else
            {
                for (int i = 0; i < files.length; i++)
                {
                    deleteDir(files[i].getAbsolutePath());
                }
                file.delete();
            }
        }
    }

}
